package design.pattern.Lu16ObserverPattern;

import java.util.ArrayList;
import java.util.List;

/**
 * @program JavaBase
 * @description: 观察者模式
 * @author: zhanglu
 * @create: 2020-04-10 09:57:00
 */
public class ObserverPattern03 {

    public static void main(String[] args) {

        LiSi03 liSi03 = new LiSi03();
        WangSi03 wangSi03 = new WangSi03();

        HanFeiZi03 hanFeiZi03 = new HanFeiZi03();
        hanFeiZi03.addObserver(liSi03);
        hanFeiZi03.addObserver(wangSi03);

        hanFeiZi03.haveBreakfast();
        hanFeiZi03.haveBreakfast();
        hanFeiZi03.haveBreakfast();
        hanFeiZi03.haveFun();
    }

}

interface Observable03{
    void addObserver(Observer03 observer03);
    void deleteObserver(Observer03 observer03);
    void nofifyObservers(String context);
}

interface Observer03{
    void update(String context);
}

interface IHanFeiZi03{
    void haveBreakfast();
    void haveFun();
}

class HanFeiZi03 implements IHanFeiZi03, Observable03{

    private List<Observer03> observer03List = new ArrayList<>();

    @Override
    public void addObserver(Observer03 observer03) {
        this.observer03List.add(observer03);
    }

    @Override
    public void deleteObserver(Observer03 observer03) {
        this.observer03List.remove(observer03);
    }

    @Override
    public void nofifyObservers(String context) {
        for (Observer03 observer03 : observer03List) {
            observer03.update(context);
        }
    }

    @Override
    public void haveBreakfast() {
        System.out.println("韩非：吃饭了");
        this.nofifyObservers("韩非在吃饭");
    }

    @Override
    public void haveFun() {
        System.out.println("韩非：我娱乐了");
        this.nofifyObservers("韩非在娱乐");
    }

}

class LiSi03 implements Observer03{

    @Override
    public void update(String context) {
        System.out.println("李斯监控：" + context);
    }
}

class WangSi03 implements Observer03{

    @Override
    public void update(String context) {
        System.out.println("王思监控：" + context);
    }
}

